<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addpartments('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!--default-expand-all 默认展开节点  -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addpartments(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="Editpartments(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="Delpartments(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog
          :title=" isEdit ? '编辑' : '添加'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <addor-edit v-if="showDialog" :id="curId" :orign-list="orignList" :is-edit="isEdit" @close="closedialog" @success="hsuccess" />
        </el-dialog>
        <!-- <el-dialog
          title="添加或编辑"
          :visible.sync="EditshowDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <addor-edit v-if="EditshowDialog" :id="curId" :is-edit="isEdit" @success="hsuccess" />
        </el-dialog> -->
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
// 引入转换为树形结构的 函数
import { tranListToTreeData } from '@/utils/index.js'
// 导入弹层组件
import AddorEdit from './deptDialog.vue'
export default {
  // 注册组件
  components: {
    AddorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 添加 默认不显示
      showDialog: false,
      // 编辑默认不显示
      // EditshowDialog: false,
      // 点击的地方
      curId: '',
      // 给个默认添加状态
      isEdit: false,
      // 默认数据
      orignList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hsuccess() {
      // 关闭弹框，重新渲染
      this.showDialog = false
      // this.EditshowDialog = false
      this.loadDepartments()
    },
    // 查询企业部门列表
    async loadDepartments() {
      const res = await getDepartments()
      // console.log(res)
      // 删除传智教育的数据
      res.data.depts.shift()
      // 数组遍历 map 方法会返回一个新的数组，map 不会改变原来的数组
      this.orignList = res.data.depts.map(item => {
        return { name: item.name, code: item.code, pid: item.pid, id: item.id }
      })
      // 渲染页面数据,转换为树形结构
      this.list = tranListToTreeData(res.data.depts)
    },
    // 添加
    addpartments(id) {
      // 点击添加的时候显示弹层
      this.showDialog = true
      // 传id给子组件
      this.curId = id
      // 添加状态
      this.isEdit = false
    },
    // 编辑
    Editpartments(id) {
      // 点击编辑的时候显示弹层
      this.showDialog = true
      // 传id给子组件
      this.curId = id
      // 编辑状态
      this.isEdit = true
    },
    // 删除
    Delpartments(id) {
      this.$confirm('你确定要删除吗, 嘤嘤嘤', '提示', {
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        // console.log(res)
        this.$message.success(res.message)
        // 重新渲染页面数据
        this.loadDepartments()
      }).catch(() => {

      })
    },
    // 关闭弹框
    closedialog() {
      this.showDialog = false
    }
  }
}
</script>
